/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/network_port.h"
#include <iomanip>

namespace dev {

std::string mac_addr_to_hex_string(const uint8_t* mac_addr, size_t len) {
    std::stringstream ss;
    for (size_t i = 0; i < len; ++i) {
        if (i > 0) {
            ss << ":";
        }
        ss << std::hex << std::uppercase << std::setfill('0') << std::setw(2)
           << static_cast<unsigned>(mac_addr[i]);
    }
    return ss.str();
}

// bool NetworkPort::update_bdf_by_ncsi() {
//     return true;
// }

bool NetworkPort::SendNcsiTestPacket(uint8_t cmd_code, const std::string& req_data) {
    // 检查NCSI连接是否可用
    if (!m_ncsi_over_mctp_wx) {
        elog("Error: NCSI over MCTP not initialized");
        return false;
    }

    try {
        // 发送NCSI测试包
        bool ret =
            m_ncsi_over_mctp_wx->send_test_packet(DEFAULT_PACKAGE_ID, PortId, cmd_code, req_data);
        if (!ret) {
            elog("Error: Failed to send NCSI test packet");
            return false;
        }

        return true;
    } catch (const std::exception& e) {
        elog("Error sending NCSI test packet: ${error}", ("error", e.what()));
        return false;
    }
}

bool NetworkPort::SetLLDPTxEnable(bool enable) {
    return true;
}

bool NetworkPort::update_mac_addr_by_ncsi() {
    uint8_t mac_addr[6] = {};
    bool    ret = m_ncsi_over_mctp_wx->get_mac_address(DEFAULT_PACKAGE_ID, PortId, mac_addr, 6);
    if (!ret) {
        elog("Error: Failed to update MAC address");
        return false;
    }
    std::string mac_addr_str = mac_addr_to_hex_string(mac_addr, 6);
    MACAddress.set_value(mac_addr_str);
    return true;
}

// bool NetworkPort::update_default_mac_addr_by_ncsi() {
//     return true;
// }

bool NetworkPort::enable_lldp_capability_by_ncsi() {
    bool ret = m_ncsi_over_mctp_wx->enable_lldp(DEFAULT_PACKAGE_ID, PortId);
    if (!ret) {
        elog("Error: Failed to enable LLDP capability");
        return false;
    }
    return true;
}

bool NetworkPort::disable_lldp_capability_by_ncsi() {
    bool ret = m_ncsi_over_mctp_wx->disable_lldp(DEFAULT_PACKAGE_ID, PortId);
    if (!ret) {
        elog("Error: Failed to disable LLDP capability");
        return false;
    }
    return true;
}

bool NetworkPort::get_lldp_capability_by_ncsi() {
    uint8_t lldp_capability = 0;
    bool    lldp_enable     = LLDPEnabled.get_value().as<bool>();
    if (lldp_enable) {
        return true;
    }
    bool ret =
        m_ncsi_over_mctp_wx->get_lldp_capability(DEFAULT_PACKAGE_ID, PortId, &lldp_capability);
    if (!ret) {
        elog("Error: Failed to get LLDP capability");
        return false;
    }
    // 未使能时，使能LLDP，使能失败设置为false
    if (lldp_capability == 0) {
        if (!enable_lldp_capability_by_ncsi()) {
            LLDPEnabled.set_value(false);
            return false;
        }
    }
    // 已使能或使能成功时，设置为true
    LLDPEnabled.set_value(true);
    return true;
}

void NetworkPort::start_ncsi_update_task_wx(ncsi_over_mctp_wx_ptr ncsi_over_mctp,
                                            mc::milliseconds      interval) {
    m_ncsi_over_mctp_wx = ncsi_over_mctp;
    m_ncsi_timer        = new mc::core::timer(this);
    m_ncsi_timer->timeout.connect([this]() {
        update_mac_addr_by_ncsi();
        get_lldp_capability_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void NetworkPort::stop_ncsi_update_task() {
    m_ncsi_timer->stop();
}
} // namespace dev

MC_REFLECT(dev::NetworkPort)